﻿using System;
using System.IO;
using System.ServiceModel;
using System.ComponentModel;
using ajayumi.develop.win32.Transport.WCF.Services;

namespace ajayumi.develop.win32.Transport.Core
{
    /// <summary>
    /// 传输文件抽象类
    /// Author  : 黄乙冬
    /// Date    : 2011-08-30
    /// </summary>
    public abstract class TransFileBase : IDisposable
    {
        #region Variables

        protected delegate void TransFileTaskHander();
        protected readonly object m_LockObj = null;

        #endregion

        #region Properties

        public string FilePath
        {
            get { return Path.Combine(RootPath, RelativePath); }
        }

        public string RootPath { get; private set; }
        public string RelativePath { get; private set; }

        protected TransportAction TransportAction { get; private set; }

        #endregion


        public TransFileBase(string rootPath, string relativePath, TransportAction action)
        {
            this.RootPath = rootPath;
            this.RelativePath = relativePath;
            this.TransportAction = action;

            if (string.IsNullOrEmpty(this.FilePath))
            { throw new Exception("文件路径不能为空！"); }

            this.m_LockObj = new object();
        }

        #region Events
        public event EventHandler<TransReadyEventArgs> TransReady;
        public event EventHandler<TransCompletedEventArgs> TransCompleted;
        public event EventHandler<TransProgressChangedEventArgs> TransProgressChanged;


        #region OnTransReady
        /// <summary>
        /// Triggers the TransReady event.
        /// </summary>
        public virtual void OnTransReady(TransReadyEventArgs ea)
        {
            if (TransReady != null)
                TransReady(this, ea);
        }
        #endregion
        #region OnTransCompleted
        /// <summary>
        /// Triggers the TransCompleted event.
        /// </summary>
        public virtual void OnTransCompleted(TransCompletedEventArgs e)
        {
            if (TransCompleted != null)
                TransCompleted(this, e);
        }
        #endregion
        #region OnTransProgressChanged
        /// <summary>
        /// Triggers the TransProgressChanged event.
        /// </summary>
        public virtual void OnTransProgressChanged(TransProgressChangedEventArgs ea)
        {
            if (TransProgressChanged != null)
                TransProgressChanged(this, ea);
        }
        #endregion

        #endregion

        public virtual void RunWork(object state)
        {
            this.TransFileTask();
        }

        protected virtual void TransFileTask()
        {
            this.BeginTransFileTask();
        }


        public virtual void RunWorkAsync()
        {
            this.RunWorkAsync(null);
        }

        public virtual void RunWorkAsync(object state)
        {
            TransFileTaskHander task = new TransFileTaskHander(this.BeginTransFileTask);
            task.BeginInvoke(new AsyncCallback(this.EndTransFileTask), task);
        }

        protected abstract void BeginTransFileTask();

        protected virtual void EndTransFileTask(IAsyncResult ar)
        {
            TransFileTaskHander task = ar.AsyncState as TransFileTaskHander;
            task.EndInvoke(ar);
        }

        public virtual void Dispose()
        {
            //try
            //{
            //    this.m_TransFileServiceClient.Close();
            //}
            //catch (FaultException ex)
            //{ this.m_TransFileServiceClient.Abort(); }
            //catch (TimeoutException ex)
            //{ this.m_TransFileServiceClient.Abort(); }
            //catch (Exception ex)
            //{
            //    this.m_TransFileServiceClient.Abort();
            //    throw;
            //}
        }
    }
}
